package com.xph.core.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.xph.core.common.util.CommUtil;
import com.xph.core.common.util.MyBatisSql;

public class CustomSqlSessionDaoSupport extends SqlSessionDaoSupport {
    private static Logger log = Logger.getLogger(CustomSqlSessionDaoSupport.class);
            
    public List selectList(String mapper, Map map)
    {
        log.info("selectList start: mapper="+mapper+", map:"+(map==null?null:map.toString()));
        List list;
        if(map.containsKey("total"))
        {
            //如果是计算记录总数
            MyBatisSql myBatisSql = new MyBatisSql();
            String sql = myBatisSql.getMyBatisSql(map, mapper, super.getSqlSession()).getSql();
            sql = sql.replaceFirst("\\s*select[\\S|\\s]+?from", "select count(1) as total from");
            if(sql.indexOf("order by")>-1)
            {
                sql = sql.substring(0, sql.indexOf("order by"));
            }
            sql = removeTabAndEnter(sql);
            log.debug("CustomSqlSessionDao.java-sql(counttotal):" + sql);
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("sql", sql);
            list = super.getSqlSession().selectList("public.getSqlMap", paraMap);
        }
        else if(map.containsKey("pageno"))
        {
            //如果需要分页，则先获取Mapper映射文件中的语句，然后再用语句调用分页存储过程
            MyBatisSql myBatisSql = new MyBatisSql();
            String sql = myBatisSql.getMyBatisSql(map, mapper, super.getSqlSession()).getSql();
            sql = removeTabAndEnter(sql);
            log.debug("CustomSqlSessionDao.java-sql(page):" + sql);
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("sql", sql);
            paraMap.put("startIndex", Integer.parseInt(map.get("pageno").toString()));
            paraMap.put("pageSize", Integer.parseInt(map.get("limit").toString()));
            list = super.getSqlSession().selectList("public.getListPage", paraMap);
        }
        else
        {
            list = super.getSqlSession().selectList(mapper, map);
            
        }
        list = CommUtil.lowerListMapKey(list);
        return list;
    }

    /**
     * 去掉回车、tab和多余的空格
     * @param sql
     * @return
     */
    private String removeTabAndEnter(String sql)
    {
        sql = sql.replace("\t", " ");
        sql = sql.replace("\n", " ");
        sql = sql.replaceAll("\\s{2,}", " ");
        return sql;
    }
    
    public Map selectMap(String mapper, Map map){
        log.info("selectMap start: mapper="+mapper+", map:"+(map==null?null:map.toString()));
        map = (Map)super.getSqlSession().selectOne(mapper, map);
        map = CommUtil.lowerMapKey(map);
        return map;
    }
        
    public Object selectOne(String arg0, Object arg1){
        if(arg0!=null&&arg1!=null){
            log.info("selectOne start: arg0="+arg0.toString()+", arg1:"+arg1.toString());
        }
        return super.getSqlSession().selectOne(arg0, arg1);
    }   
    
    
    public static void main(String[] args)
    {
        String s = "select a,b,c from (select * from xxxx)";
        System.out.println(s.replaceFirst("\\s*select.*?from", "select cout(1) from"));
    }
}
